package com.smart.list;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value="/", method=RequestMethod.GET)
	public ModelAndView home() {
		logger.info("Welcome home!");
		ModelAndView model = new ModelAndView("home");
		model.addObject("msg", new Date().toString());
		return model;
	}
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value="/tasks", method=RequestMethod.GET)
	public ModelAndView task(HttpServletRequest request) {
		logger.info("enter tasks!");
		String user = request.getRemoteUser();
		ModelAndView model = new ModelAndView("tasks");
		if (user!=null)
			model.addObject("username", user);
		return model;
	}
	
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public ModelAndView login() {
		logger.info("user loged in!");
		ModelAndView model = new ModelAndView("login");
		return model;
	}
	
	@RequestMapping(value="/error", method=RequestMethod.POST)
	public ModelAndView error() {
		logger.info("error login");
		ModelAndView model = new ModelAndView("error");
		return model;
	}
	
	@RequestMapping(value="/logout", method=RequestMethod.GET)
	public ModelAndView logout(HttpServletRequest request) {
		logger.info("logout user");
		request.getSession().invalidate();
		ModelAndView model = new ModelAndView("home");
		return model;
	}	
}

